package testing_package;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;

public class nbServer {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// Create the server socket channel
		ServerSocketChannel server = ServerSocketChannel.open();
		// nonblocking I/O
		server.configureBlocking(false);
		// host-port 8000
		server.socket().bind(new java.net.InetSocketAddress("localhost",8000));
		System.out.println("Server attivo porta 8000");
		// Create the selector
		Selector selector = Selector.open();
		// Recording server to selector (type OP_ACCEPT)
		server.register(selector,server.validOps());
		
		// Infinite server loop
		for(;;) {
		  // Waiting for events
		  selector.select();
		  // Get keys
		  Set keys = selector.selectedKeys();
		  Iterator i = keys.iterator();		
		  

		  // For each keys...
		  while(i.hasNext()) {
			
		    SelectionKey key = (SelectionKey) i.next();
			  //SelectionKey key = selector.selectedKeys().iterator().next();

		    // Remove the current key
		    i.remove();

		    // if isAccetable = true
		    // then a client required a connection
		    if (key.isAcceptable()) {
		      // get client socket channel
		      SocketChannel client = server.accept();
		      // Non Blocking I/O
		      client.configureBlocking(false);
		      // recording to the selector (reading)
		      client.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
		      
		      continue;
		    }

		    // if isReadable = true
		    // then the server is ready to read 
		    if (key.isReadable()) {

		      SocketChannel client = (SocketChannel) key.channel();

		      // Read byte coming from the client
		      int BUFFER_SIZE = 32;
		      ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
		      try {
		        client.read(buffer);
		      }
		      catch (Exception e) {
		        // client is no longer active
		        e.printStackTrace();
		        continue;
		      }

		      // Show bytes on the console
		      buffer.flip();
		      Charset charset=Charset.forName("ISO-8859-1");
		      CharsetDecoder decoder = charset.newDecoder();
		      CharBuffer charBuffer = decoder.decode(buffer);
		      Calendar now = Calendar.getInstance();		      
		      if (charBuffer.length() > 0)
		    	  System.out.print(now.get(Calendar.MINUTE) + " " + now.get(Calendar.SECOND) + " " + charBuffer.toString() + "\n");
		      
		      continue;
		    }
		  }
		}
		
		

	}

}
